// pages/index/comps/crowdFunding/crowdFunding.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        crowdInfo: Object,
        nowDate: String
    },

    /**
     * 组件的初始数据
     */
    data: {
        remainDay: 0,
        remainHour: 0,
        remainMinute: 0,
        remainSecond: 0,
        overTime: false
    },

    /**
     * 组件的方法列表
     */
    methods: {
        getTime: function() {
            let today = new Date(),
                h = today.getHours(),
                m = today.getMinutes(),
                s = today.getSeconds()
            let stopTime = new Date(this.data.crowdInfo.end_time.replace(/-/g, '/')),
                stopH = stopTime.getHours(),
                stopM = stopTime.getMinutes(),
                stopS = stopTime.getSeconds()
            let remainTime = stopTime.getTime() - today.getTime()
            if (remainTime < 0) {
                this.setData({
                    overTime: true
                })
                clearInterval(this.time)
                return
            }
            let remainDay = parseInt(remainTime / (60 * 60 * 24 * 1000))
            this.setData({
                remainDay: remainDay
            })
            let remainTimeD =
                parseInt(remainTime) -
                parseInt(remainDay * 60 * 60 * 24 * 1000)
            let remainHour = parseInt(remainTimeD / (60 * 60 * 1000))
            this.setData({
                remainHour: remainHour
            })
            let remainTimeH = remainTimeD - remainHour * 60 * 60 * 1000
            let remainMinute = parseInt(remainTimeH / (60 * 1000))
            this.setData({
                remainMinute: remainMinute
            })
            let remainTimeM = remainTimeH - remainMinute * 60 * 1000
            let remainSecond = parseInt(
                (remainTime -
                    remainDay * 60 * 60 * 24 * 1000 -
                    remainHour * 60 * 60 * 1000 -
                    remainMinute * 60 * 1000) /
                1000
            )
            this.setData({
                remainSecond: remainSecond
            })
        },
        goCrowdFundingDetail: function(e) {
            wx.navigateTo({
                url: '/pages/crowdFunding/detail/detail?crowdFundingId=' + e.currentTarget.dataset.id
            })
        }
    },

    attached: function () {
        const that = this
        this.getTime()
        this.time = setInterval(function () {
            that.getTime()
        }, 1000)
    }
})